﻿@model QuanLyGiaoVuUI.Models.QuanLySinhVien.NhapDanhSachSinhVienViewModel

@{
    Layout = null;
    }

<style>
    .hiddenField {
        display: none;
    }
</style>
<script src="@Url.Content("~/Scripts/jquery.form.js")"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

    <form id="uploadFile" name="UploadFile" action="/QuanLySinhVien/NhapDanhSachSinhVien" method="post">
        <table border="0" cellpadding="0" cellspacing="0" id="id-form" class="form">
        <tr>
            <th valign="top"><label>Chọn file mẫu</label></th>
            <td>@Html.DropDownList("FileMau", new SelectList(Model.danhSachFileMau, "MaTapTin","TenTaiLieu"))</td>
        </tr>
        <tr>
            <th valign="top">Chọn file dữ liệu</th>
            <td><input type="file" name="fileDuLieu" /></td>
        </tr>
    </table>
    <div id="loadingImage" class="hiddenField ui-overlay">
        <img src="~/Images/gif/loading-bar.gif" /></div>
    <input type="submit" id="submitForm" style="display: none;" />
    <input type="hidden" id="filename"/>
</form>    

<script>
    $(document).ready(function () {
        $("input:file").change(function () {
            $("#filename").val($(this).val());
        });

        $("#uploadFile").ajaxForm({
            dataType: 'json',
            success: OnSuccessCallBack,
            beforeSubmit: beforeUploadFile,
            error: uploadFileFail,
            uploadProgress: showProgress
        });
    });

    function uploadFileFail() {
        $("#loadingImage").addClass("hiddenField");
        ToastManager.showStickyWarningToast("Không thể upload file.");
    }

    function showProgress(event, position, total, percentComplete) {
        $("#position").val(position);
    }

    function SubmitNhapDuLieu() {
        if ($("#filename").val().length > 0)
            $("#submitForm").click();
        else
            ToastManager.showStickyWarningToast("Chưa chọn file.");
    }

    function beforeUploadFile() {
        $("#loadingImage").removeClass("hiddenField");
    }

    function OnSuccessCallBack(data) {
        if (data.success) {
            ToastManager.showSuccessToast("Nhập dữ liệu thành công.");
            var myTimeout = setTimeout(function () {
                location.reload(true);
            }, 1000);
        }
        else {
            ToastManager.showStickyWarningToast(data.message);
            $("#loadingImage").addClass("hiddenField");
        }
    }
</script>